This tutorial explains what is Meta Data Type.
Assume that we have Class Person.
This is called a Data Type (or just Type for short) because it describes type of data (but not the actual values).
By Type of Data we mean collection of following data: name, age, weight (which is considered as composite data).
When you create Instance John you specify specific data (provide values for each variable).
Then Person.Type is Meta Type because it describes Type Class Person.
For instance it describes Class Person by being aware of all the init() Constructors declared in it.
By using Person.self you create instance of Metatype Person.Type.
Through this instance you now have access to init() Constructors which are otherwise inaccessible.
There is only one Person.self Instance (since you can't specify different data for its Fields).
It is all about accessing Class Person Methods (so this feature provides reflection functionality).
By providing Person.self Instance as parameter to a Function, this function can now create instances of Class Person by
directly calling its init() Constructors.
Test.swift
class Person {
var name : String
required init(name: String) { self.name = name }
}
func createPerson (metatypeInstance: Person.Type) -> Person {
return metatypeInstance.init(name: "John")
}
var john : Person = createPerson(metatypeInstance: Person.self)
var bill : Person = Person.self.init(name: "Bill")
Possible error
Constructing an object of class type 'Person' with a metatype value must use a 'required' initializer
Type and Meta Type